﻿using OrderSystem.Common;
using OrderSystem.DataAccess;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace OrderSystem
{
    public partial class FormSelectCustomer : Form
    {
        private static CustomerData s_customerInfo = null;

        public FormSelectCustomer()
        {
            InitializeComponent();
            FormSelectCustomer.s_customerInfo = null;
        }

        private void FormSelectCustomer_Load(object sender, EventArgs e)
        {
            this.cbCondition.SelectedIndex = 0;
            string strQuery = "SELECT PKID AS 序号,CompanyName AS 公司,Linkman AS 联系人,Type 客户类型 FROM Customer ORDER BY PKID";
            Customers customers = new Customers();
            DataTable dataTable = null;
            try
            {
                dataTable = customers.GetCustomers(strQuery);
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            catch (Exception ex2)
            {
                MessageBox.Show(ex2.Message + "\r\n出现未知错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            customers.Dispose();
            if (dataTable != null)
            {
                this.dgCustomer.DataSource = null;
                this.dgCustomer.DataSource = dataTable;
                this.dgCustomer.AutoResizeColumns();
                dataTable.Dispose();
            }
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            string text = "SELECT PKID AS 序号,CompanyName AS 公司,Linkman AS 联系人,Type 客户类型 FROM Customer";
            if (this.cbCondition.SelectedIndex == 0)
            {
                text = text + " WHERE dbo.GetPY(CompanyName) LIKE '%" + this.txtCondition.Text.Trim().Replace("'", "''").Replace(" ", "%") + "%'";
            }
            if (this.cbCondition.SelectedIndex == 1)
            {
                text = text + " WHERE dbo.GetPY(CompanyAddress) LIKE '%" + this.txtCondition.Text.Trim().Replace("'", "''").Replace(" ", "%") + "%'";
            }
            if (this.cbCondition.SelectedIndex == 2)
            {
                text = text + " WHERE dbo.GetPY(Linkman) LIKE '%" + this.txtCondition.Text.Trim().Replace("'", "''").Replace(" ", "%") + "%'";
            }
            text += " ORDER BY PKID";
            Customers customers = new Customers();
            DataTable dataTable = null;
            try
            {
                dataTable = customers.GetCustomers(text);
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            catch (Exception ex2)
            {
                MessageBox.Show(ex2.Message + "\r\n出现未知错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            customers.Dispose();
            if (dataTable != null)
            {
                this.dgCustomer.DataSource = null;
                this.dgCustomer.DataSource = dataTable;
                this.dgCustomer.AutoResizeColumns();
                dataTable.Dispose();
            }
        }

        private void txtCondition_Enter(object sender, EventArgs e)
        {
            base.AcceptButton = this.btnQuery;
        }

        private void cbCondition_Enter(object sender, EventArgs e)
        {
            base.AcceptButton = this.btnQuery;
        }

        private void dgCustomer_Enter(object sender, EventArgs e)
        {
            base.AcceptButton = this.btnOk;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (this.dgCustomer.Rows.Count > 0)
            {
                int rowIndex = this.dgCustomer.CurrentCell.RowIndex;
                FormSelectCustomer.s_customerInfo = new CustomerData(Convert.ToInt32(this.dgCustomer.Rows[rowIndex].Cells[0].Value), this.dgCustomer.Rows[rowIndex].Cells[1].Value.ToString(), this.dgCustomer.Rows[rowIndex].Cells[2].Value.ToString());
            }
            else
            {
                MessageBox.Show("表格中没有数据！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            base.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        public static CustomerData ShowBox()
        {
            FormSelectCustomer formSelectCustomer = new FormSelectCustomer();
            formSelectCustomer.ShowDialog();
            return FormSelectCustomer.s_customerInfo;
        }

        private void dgCustomer_DoubleClick(object sender, EventArgs e)
        {
            this.btnOk_Click(null, null);
        }

        private void dgCustomer_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Return)
            {
                this.btnOk_Click(null, null);
            }
            if (e.KeyCode == Keys.Up && (this.dgCustomer.CurrentRow == null || this.dgCustomer.CurrentRow.Index < 1))
            {
                this.txtCondition.Focus();
            }
        }

        private void txtCondition_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Down)
            {
                this.dgCustomer.Focus();
            }
        }
    }
}
